package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.consts.DaoConst;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.entity.*;
import yf.mall.enums.MallActStatusEnum;
import yf.mall.enums.MallTypeTypeEnum;
import yf.mall.service.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/mallType")
@RestController
public class ApiMallTypeController {

    @Autowired
    private MallTypeService mallTypeService;
    @Autowired
    private MallGoodsService mallGoodsService;
    @Autowired
    private SaleTypeInfoService saleTypeInfoService;

    /**
     * 分类展示
     *
     * @return
     */
    @RequestMapping("/list")
    @IgnoreAuth
    public R queryList() {
        return R.ok(mallTypeService.findAll());
    }

    /**
     * 根据分类id查询产品
     *
     * @param id    分类id
     * @param page  当前页
     * @param limit 每页显示条数
     * @return
     */
    @RequestMapping("/queryMallGoods/{id}/{page}/{limit}")
    public R queryMallGoods(@PathVariable(name = "id") long id,
                            @PathVariable(name = "page") String page,
                            @LoginUserId Long userId,
                            @PathVariable(name = "limit") String limit) {

        HashMap<String, Object> map = new HashMap<>();  //设置查询条件
        map.put("page", page);
        map.put("limit", limit);
        map.put("typeId", id);
        PageHelper.startPage(DaoConst.getPage(map), DaoConst.getLIMIT(map)); //分页查询

        List<MallGoodsEntity> mallGoodsEntities = mallGoodsService.queryMallGoods(map);  //根据条件查询产品
        //获取活动
        for (MallGoodsEntity mallGoodsEntity : mallGoodsEntities) {
            SaleTypeInfoEntity saleTypeInfoEntity = saleTypeInfoService.findByGoodsId(mallGoodsEntity.getId());
            if (null!= saleTypeInfoEntity){
                mallGoodsEntity.setAct(saleTypeInfoEntity);
            }
        }
        //获取所属店铺
        getShop(mallGoodsEntities);
        mallGoodsEntities = mallGoodsService.filterByLevel(userId,mallGoodsEntities);
        return R.ok(mallGoodsEntities);
}

    @Autowired
    private ShopService shopService;
    //quequnlong 获取所属店铺
    private void getShop(List<MallGoodsEntity> mallGoodsEntities) {
        for (MallGoodsEntity mallGoodsEntity : mallGoodsEntities) {
            Long userId = mallGoodsEntity.getUserId();
            //不是公司的产品
            if (userId != null && !userId.equals(Const.ADMIN_ID)){
                ShopEntity shopEntity = shopService.findByUserId(userId);
                if (shopEntity != null){
                    mallGoodsEntity.setShopEntity(shopEntity);
                }
            }
        }
    }

    @Autowired
    private LotteryActService lotteryActService;

    /** quequnlong
     *  首页tab栏的分类展示
     */
    @RequestMapping("/findBigType")
    @IgnoreAuth
    public R findBigType(){
        List<MallTypeEntity> list = mallTypeService.findType();
        //首页显示的二级分类
        List<MallTypeEntity> home = new ArrayList<>();
        if (null != list || list.size() > 0){
            for (MallTypeEntity mallTypeEntity : list) {
                if (mallTypeEntity.getChildren().size() > 0 && mallTypeEntity.getChildren() != null){
                    home.add(mallTypeEntity.getChildren().get(0));
                }
            }
        }
        Boolean flag = lotteryActService.lotteryAct();
        return R.ok(list).putExtra("home",home).putExtra("flag",flag);
    }
}
